home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDIB.PAK / BRUSHDLG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  455 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   brushdlg.c
  9. //
  10. //  PURPOSE:   Displays the "Brush Style" dialog box
  11. //
  12. //  FUNCTIONS:
  13. //    BrushDlg        - Process messages for "Brush Style" dialog box.
  14. //    MsgBrushInit    - Initialize the Brush dialog with info from lparam.
  15. //    MsgBrushPaint   - Paint the Example Window in the Brush dialog
  16. //    MsgBrushCommand - Process WM_COMMAND messages sent to the Brush dialog.
  17. //    CmdBrushStyle   - Track the currently selected brush style.
  18. //    CmdBrushHatch   - Track the currently selected hatch style.
  19. //    CmdBrushColor   - Put up the ChooseColor dialog to select brush color.
  20. //    CmdBrushDone    - Free the Brush dialog and related data.
  21. //
  22. //  COMMENTS:
  23. //
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #include <windowsx.h>
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "brushdlg.h"           // Controls ID's for the Brush dialog
  29. #include "colordlg.h"           // palette color dialog defines/prototypes
  30. #include "palette.h"            // palette routine prototypes and defines
  31.  
  32. // global variables specific to this module
  33. RECT rcExample;                 // location of example window in dialog
  34.  
  35. // prototypes specific to this module
  36. LRESULT MsgBrushCommand (HWND, UINT, WPARAM, LPARAM);
  37. LRESULT MsgBrushInit    (HWND, UINT, WPARAM, LPARAM);
  38. LRESULT MsgBrushPaint   (HWND, UINT, WPARAM, LPARAM);
  39. LRESULT CmdBrushStyle   (HWND, WORD, WORD, HWND);
  40. LRESULT CmdBrushHatch   (HWND, WORD, WORD, HWND);
  41. LRESULT CmdBrushColor   (HWND, WORD, WORD, HWND);
  42. LRESULT CmdBrushDone    (HWND, WORD, WORD, HWND);
  43.  
  44. // Brush dialog message table definition.
  45. MSD rgmsdBrush[] =
  46. {
  47.     {WM_COMMAND,    MsgBrushCommand},
  48.     {WM_PAINT,      MsgBrushPaint},
  49.     {WM_INITDIALOG, MsgBrushInit}
  50. };
  51.  
  52. MSDI msdiBrush =
  53. {
  54.     sizeof(rgmsdBrush) / sizeof(MSD),
  55.     rgmsdBrush,
  56.     edwpNone
  57. };
  58.  
  59. // Brush dialog command table definition.
  60. CMD rgcmdBrush[] =
  61. {
  62.     {IDD_SOLIDBRUSH,    CmdBrushStyle}, // Brush Style notifications
  63.     {IDD_NULLBRUSH,     CmdBrushStyle},
  64.     {IDD_HATCHBRUSH,    CmdBrushStyle},
  65.     {IDD_HATCHSTYLE,    CmdBrushHatch}, // Hatch Style notification
  66.     {IDD_BRUSHCOLOR,    CmdBrushColor}, // Color button
  67.     {IDOK,              CmdBrushDone},  // OK and Cancel buttons
  68.     {IDCANCEL,          CmdBrushDone}
  69. };
  70.  
  71. CMDI cmdiBrush =
  72. {
  73.     sizeof(rgcmdBrush) / sizeof(CMD),
  74.     rgcmdBrush,
  75.     edwpNone
  76. };
  77.  
  78.  
  79. //
  80. //  FUNCTION: BrushDlg(HWND, UINT, WPARAM, LPARAM)
  81. //
  82. //  PURPOSE:  Processes messages for "Brush Style" dialog box.
  83. //
  84. //  PARAMETERS:
  85. //    hdlg - window handle of the dialog box
  86. //    wMessage - type of message
  87. //    wparam - message-specific information
  88. //    lparam - message-specific information
  89. //
  90. //  RETURN VALUE:
  91. //    TRUE - message handled
  92. //    FALSE - message not handled
  93. //
  94. //  COMMENTS:
  95. //
  96. //
  97.  
  98. LRESULT CALLBACK BrushDlg(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  99. {
  100.     return DispMessage(&msdiBrush, hdlg, uMessage, wparam, lparam);
  101. }
  102.  
  103.  
  104. //
  105. //  FUNCTION: MsgBrushCommand(HWND, UINT, WPARAM, LPARAM)
  106. //
  107. //  PURPOSE: Process WM_COMMAND messages sent to the Brush dialog.
  108. //
  109. //  PARAMETERS:
  110. //    hwnd      - The window handing the message.
  111. //    uMessage  - The message number. (unused).
  112. //    wparam    - Message specific data (unused).
  113. //    lparam    - Message specific data (unused).
  114. //
  115. //  RETURN VALUE:
  116. //    Always returns 0 - message handled.
  117. //
  118. //  COMMENTS:
  119. //    Uses this DipsCommand function defined in wndproc.c combined
  120. //    with the cmdiBrush structure defined in this file to handle
  121. //    the command messages for the Brush dialog box.
  122. //
  123.  
  124. #pragma argsused
  125. LRESULT MsgBrushCommand(HWND   hwnd,
  126.                         UINT   uMessage, 
  127.                         WPARAM wparam, 
  128.                         LPARAM lparam)
  129. {
  130.     return DispCommand(&cmdiBrush, hwnd, wparam, lparam);
  131. }
  132.  
  133.  
  134. //
  135. //  FUNCTION: MsgBrushInit(HWND, UINT, WPARAM, LPARAM)
  136. //
  137. //  PURPOSE: To initialize the Brush dialog with info from lparam.
  138. //
  139. //  PARAMETERS:
  140. //    hwnd - The window handing the message.
  141. //    uMessage - The message number. (unused).
  142. //    wparam - Message specific data (unused).
  143. //    lparam - points to LOGBRUSH structure.
  144. //
  145. //  RETURN VALUE:
  146. //    Always returns TRUE
  147. //
  148. //  COMMENTS:
  149. //    Sets the initial state of the controls according to the LOGBRUSH
  150. //    passed in via lparam.
  151. //
  152.  
  153. #pragma argsused
  154. LRESULT MsgBrushInit(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  155. {
  156.      int i, nIndex, nSel;
  157.     HWND hctlHatch;
  158.     char szTmp[32];
  159.     LPLOGBRUSH lpLB;
  160.  
  161.     // lparam is a pointer to a LOGBRUSH structure
  162.     lpLB = (LPLOGBRUSH)lparam;
  163.  
  164.     // Save pointer to LOGBRUSH structure in window bytes
  165.     SetWindowLong(hdlg, DWL_USER, (LONG)lpLB);
  166.  
  167.     // Check the correct brush style button
  168.      CheckRadioButton(hdlg,
  169.                      IDD_BRUSHFIRST,
  170.                      IDD_BRUSHLAST,
  171.                      IDD_BRUSHSTYLE + lpLB->lbStyle);
  172.  
  173.     // Fill up the hatch style combobox.  The item data for each item is
  174.     // set to the hatch style value defined by Windows (e.g. HS_DIAGCROSS).
  175.  
  176.     hctlHatch = GetDlgItem(hdlg, IDD_HATCHSTYLE);
  177.     nSel = 0;
  178.  
  179.     for (i = IDD_HATCHFIRST; i <= IDD_HATCHLAST; i++)
  180.      {
  181.         LoadString(hInst, i, szTmp, sizeof(szTmp));
  182.         nIndex = SendMessage(hctlHatch, CB_ADDSTRING, 0, (LPARAM)(LPSTR)szTmp);
  183.         SendMessage(hctlHatch, CB_SETITEMDATA, nIndex, i - IDD_HATCHSTYLE);
  184.  
  185.         // If this item is the current style, remember it
  186.         if (i == IDD_HATCHSTYLE + (int)lpLB->lbHatch)
  187.             nSel = nIndex;
  188.     }
  189.  
  190.     // Set the initial hatch selection
  191.     SendMessage(hctlHatch, CB_SETCURSEL, nSel, 0L);
  192.  
  193.     // Center the dialog over the application window
  194.     CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  195.  
  196.     // Get coordinates of the example window in the dialog
  197.     GetWindowRect(GetDlgItem(hdlg, IDD_BRUSHEXAMPLE), &rcExample);
  198.     ScreenToClient(hdlg, (LPPOINT)&rcExample);
  199.     ScreenToClient(hdlg, ((LPPOINT)&rcExample) + 1);
  200.  
  201.     // Reduce rect slightly so we don't paint over its frame
  202.     InflateRect(&rcExample, -1, -1);
  203.  
  204.      return 1;
  205. }
  206.  
  207.  
  208. //
  209. //  FUNCTION: MsgBrushPaint(HWND, UINT, WPARAM, LPARAM)
  210. //
  211. //  PURPOSE: Paint the example window with the current brush style
  212. //
  213. //  PARAMETERS:
  214. //    hwnd - The window handling the message.
  215. //    uMessage - The message number. (unused).
  216. //    wparam - Message specific data (unused).
  217. //    lparam - Message specific data (unused).
  218. //
  219. //  RETURN VALUE:
  220. //    Always returns TRUE
  221. //
  222. //  COMMENTS:
  223. //
  224.  
  225. #pragma argsused
  226. LRESULT MsgBrushPaint(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  227. {
  228.      PAINTSTRUCT ps;
  229.      LPLOGBRUSH lpLB;
  230.      HBRUSH hbr;
  231.  
  232.     BeginPaint(hdlg, &ps);
  233.  
  234.     // Select our logical palette for palette-relative colors to work
  235.     if (hPalette)
  236.         SelectPalette(ps.hdc, hPalette, TRUE);
  237.  
  238.     // Get pointer to LOGBRUSH from window bytes
  239.     lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  240.  
  241.     // Create brush from current LOGBRUSH
  242.      hbr = CreateBrushIndirect(lpLB);
  243.  
  244.     // Paint the example window with the brush.
  245.     FillRect(ps.hdc, &rcExample, hbr);
  246.  
  247.     // Don't need the brush any more
  248.     DeleteObject(hbr);
  249.  
  250.     // De-select our logical palette from the DC
  251.     if (hPalette)
  252.         SelectPalette(ps.hdc, GetStockObject(DEFAULT_PALETTE), TRUE);
  253.  
  254.      EndPaint(hdlg, &ps);
  255.     return 0;
  256. }
  257.  
  258.  
  259. //
  260. //  FUNCTION: CmdBrushStyle(HWND, WORD, WORD, HWND)
  261. //
  262. //  PURPOSE: Keeps track of which style button is selected.
  263. //
  264. //  PARAMETERS:
  265. //    hwnd      - The window handling the command.
  266. //    wCommand  - Child control ID.
  267. //    wNotify   - Child notification code (unused).
  268. //    hwndCtrl  - NULL (unused).
  269. //
  270. //  RETURN VALUE:
  271. //    Always returns TRUE.
  272. //
  273. //  COMMENTS:
  274. //
  275.  
  276. #pragma argsused
  277. LRESULT CmdBrushStyle(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  278. {
  279.     // Get pointer to LOGBRUSH from window bytes
  280.     LPLOGBRUSH lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  281.  
  282.     // Save the style of the button that was just clicked
  283.     lpLB->lbStyle = wCommand - IDD_BRUSHSTYLE;
  284.  
  285.     // Repaint the example window
  286.      InvalidateRect(hdlg, &rcExample, TRUE);
  287.  
  288.     return TRUE;
  289. }
  290.  
  291.  
  292. //
  293. //  FUNCTION: CmdBrushHatch(HWND, WORD, WORD, HWND)
  294. //
  295. //  PURPOSE: Keeps track of which hatch style is selected.
  296. //
  297. //  PARAMETERS:
  298. //    hwnd      - The window handling the command.
  299. //    wCommand  - Child control ID (unused).
  300. //    wNotify   - Child notification code.
  301. //    hwndCtrl  - Handle to the Hatch Style combobox.
  302. //
  303. //  RETURN VALUE:
  304. //    Always returns TRUE.
  305. //
  306. //  COMMENTS:
  307. //
  308.  
  309. #pragma argsused
  310. LRESULT CmdBrushHatch(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  311. {
  312.      LPLOGBRUSH lpLB;
  313.      int nSel;
  314.  
  315.     // Update the hatch style selection if necessary.
  316.  
  317.     if (CBN_SELCHANGE == wNotify)
  318.     {
  319.         // Get pointer to LOGBRUSH from window bytes
  320.         lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  321.  
  322.         nSel = SendMessage(hwndCtrl, CB_GETCURSEL, 0, 0L);
  323.         if (CB_ERR != nSel)
  324.         {
  325.                 // Set brush style to hatched
  326.             lpLB->lbStyle = BS_HATCHED;
  327.             CheckRadioButton(hdlg,
  328.                              IDD_BRUSHFIRST,
  329.                              IDD_BRUSHLAST,
  330.                              IDD_HATCHBRUSH);
  331.  
  332.             // Save new hatch style
  333.             lpLB->lbHatch = SendMessage(hwndCtrl, CB_GETITEMDATA, nSel, 0L);
  334.  
  335.             // Repaint the example window
  336.             InvalidateRect(hdlg, &rcExample, TRUE);
  337.           }
  338.     }
  339.  
  340.     return TRUE;
  341. }
  342.  
  343.  
  344. //
  345. //  FUNCTION: CmdBrushColor(HWND, WORD, WORD, HWND)
  346. //
  347. //  PURPOSE: Puts up ChooseColor dialog to choose brush color.
  348. //
  349. //  PARAMETERS:
  350. //    hwnd      - The window handling the command.
  351. //    wCommand  - IDD_BRUSHCOLOR (unused).
  352. //    wNotify   - Child notification code (unused).
  353. //    hwndCtrl  - NULL (unused).
  354. //
  355. //  RETURN VALUE:
  356. //    Always returns TRUE.
  357. //
  358. //  COMMENTS:
  359. //
  360.  
  361. #pragma argsused
  362. LRESULT CmdBrushColor(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  363. {
  364.     LPLOGBRUSH lpLB;
  365.     static int nPalIndex=0; // stores system palette index for init'ing
  366.                             // the palette color selection dialog
  367.  
  368.     // Get pointer to LOGBRUSH from window bytes
  369.     lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  370.  
  371.     if (bPalDevice)
  372.     {
  373.         // call palette dialog, passing nPalIndex as LPARAM to initialize
  374.           // the correct selection in the dialog
  375.         DialogBoxParam(hInst, "ColorDlg", hdlg, (DLGPROC)Color, (LPARAM)nPalIndex);
  376.  
  377.         if (palinfo.index != -1)
  378.         {
  379.             // save index of color selection
  380.             nPalIndex = palinfo.index;
  381.  
  382.             if (palinfo.index >= 10 && palinfo.index <= 245)
  383.             {
  384.                 // Non-static color was chosen, so use PALETTERGB macro
  385.                 // to save new color
  386.                      lpLB->lbColor = PALETTERGB(palinfo.red, palinfo.green, palinfo.blue);
  387.             }
  388.             else
  389.             {
  390.                 // Static color was chosen, so just use RGB macro
  391.                 // to save new color
  392.                 lpLB->lbColor = RGB(palinfo.red, palinfo.green, palinfo.blue);
  393.             }
  394.         
  395.             // Repaint the example window
  396.             InvalidateRect(hdlg, &rcExample, TRUE);        
  397.         }
  398.  
  399.     }
  400.     else
  401.     {
  402.         CHOOSECOLOR  cc;
  403.         static DWORD dwCustColors[16];
  404.  
  405.         // Initialize CHOOSECOLOR struct
  406.         cc.lStructSize      = sizeof(cc);
  407.         cc.hwndOwner        = hdlg;
  408.         cc.hInstance        = NULL;
  409.         cc.rgbResult        = lpLB->lbColor;
  410.           cc.lpCustColors     = dwCustColors;
  411.         cc.Flags            = CC_RGBINIT;
  412.         cc.lCustData        = 0;
  413.         cc.lpfnHook         = NULL;
  414.         cc.lpTemplateName   = NULL;
  415.  
  416.         if (ChooseColor(&cc))
  417.         {
  418.             // Save new color
  419.             lpLB->lbColor = cc.rgbResult;
  420.  
  421.             // Repaint the example window
  422.                 InvalidateRect(hdlg, &rcExample, TRUE);
  423.         }
  424.     }
  425.  
  426.     return TRUE;
  427. }
  428.  
  429.  
  430. //
  431. //  FUNCTION: CmdBrushDone(HWND, WORD, HWND)
  432. //
  433. //  PURPOSE: Free the Brush dialog and related data.
  434. //
  435. //  PARAMETERS:
  436. //    hwnd      - The window handling the command.
  437. //    wCommand  - The command to be handled.
  438. //    hwndCtrl  - NULL (unused).
  439. //
  440. //  RETURN VALUE:
  441. //    Always returns TRUE.
  442. //
  443. //  COMMENTS:
  444. //    Calls EndDialog to finish the dialog session.
  445. //
  446.  
  447. #pragma argsused
  448. LRESULT CmdBrushDone(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  449. {
  450.     // Exit the dialog
  451.     EndDialog(hdlg, (IDOK == wCommand));
  452.  
  453.     return TRUE;
  454. }
  455.